<template>
  <div class="dashboard-container">
    <div class="app-container">
      <template>
        <el-card class="tree-card">
          <el-tabs v-model="activeName">
            <el-tab-pane label="员工管理" name="first">
              <!-- 新增角色按钮 -->
              <el-row style="height: 60px">
                <el-button
                  icon="el-icon-plus"
                  size="small"
                  type="primary"
                  @click="openadd()"
                >新增角色</el-button>
              </el-row>
              <!-- 表格 -->

              <el-table border="" :data="list">
                <el-table-column
                  align="center"
                  type="index"
                  label="序号"
                  width="120"
                />
                <el-table-column
                  align="center"
                  prop="name"
                  label="名称"
                  width="240"
                />
                <el-table-column
                  align="center"
                  prop="description"
                  label="描述"
                />
                <el-table-column align="center" label="操作">
                  <template slot-scope="scope">
                    <el-button
                      size="small"
                      type="text"
                      @click="openpermiss(scope.row)"
                    >分配权限</el-button>
                    <el-button
                      size="small"
                      type="text"
                      @click="openadd(scope.row)"
                    >编辑</el-button>
                    <el-button
                      size="small"
                      type="text"
                      @click="delrow(scope.row)"
                    >删除</el-button>
                  </template>
                </el-table-column>
              </el-table>
              <!-- 分页组件 -->
              <el-row
                type="flex"
                justify="center"
                align="middle"
                style="height: 60px"
              >
                <el-pagination
                  :current-page="page.page"
                  :page-size="page.pagesize"
                  :total="page.total"
                  layout="prev, pager, next"
                  @current-change="changePage"
                />
              </el-row>
              <!-- //公司信息 -->
            </el-tab-pane>
            <el-tab-pane label="公司信息" name="second">
              <el-alert
                title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
                type="info"
                show-icon
                :closable="false"
              />
              <el-form
                v-model="formData"
                label-width="120px"
                style="margin-top: 50px"
              >
                <el-form-item label="公司名称">
                  <el-input
                    v-model="formData.name"
                    disabled
                    style="width: 400px"
                  />
                </el-form-item>
                <el-form-item label="公司地址">
                  <el-input
                    v-model="formData.companyAddress"
                    disabled
                    style="width: 400px"
                  />
                </el-form-item>
                <el-form-item label="邮箱">
                  <el-input
                    v-model="formData.mailbox"
                    disabled
                    style="width: 400px"
                  />
                </el-form-item>
                <el-form-item label="备注">
                  <el-input
                    v-model="formData.remarks"
                    type="textarea"
                    :rows="3"
                    disabled
                    style="width: 400px"
                  />
                </el-form-item>
              </el-form>
            </el-tab-pane>
          </el-tabs>
        </el-card>
      </template>
    </div>
    <add-person ref="add" @addRole="getRoleList()" @chang="thisRow = {}" />
    <permiss ref="per" />
  </div>
</template>

<script>
import {
  getRoleListAPI,
  getCompanyInfoAPI,
  deleteRoleAPI,
  getRoleDetailAPI
  // changePermidsAPI
  // getpermissAPI
} from '@/api'
import { mapGetters } from 'vuex'
import addPerson from './components/addPerson.vue'
import permiss from './components/permissions.vue'
export default {
  components: { addPerson, permiss },
  data() {
    return {
      activeName: 'first',
      formData: '',

      list: [], // 承接数组
      page: {
        // 放置页码及相关数据
        page: 1,
        pagesize: 10,
        total: 0 // 记录总数
      }
    }
  },
  computed: {
    ...mapGetters(['companyId'])
  },
  async created() {
    this.getRoleList()
    this.formData = await getCompanyInfoAPI(this.companyId)
  },
  methods: {
    async getRoleList() {
      const { total, rows } = await getRoleListAPI(this.page)
      this.page.total = total
      this.list = rows
      // console.log(total, rows)
    },
    changePage(newPage) {
      // newPage是当前点击的页码
      this.page.page = newPage // 将当前页码赋值给当前的最新页码
      this.getRoleList()
    },
    // 打开新增角色
    openadd(row) {
      if (row) {
        this.$refs.add.formData = { ...row }
      }

      this.$refs.add.showDialog = true
    },
    // 删除角色
    delrow(row) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          await deleteRoleAPI(row.id)

          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.getRoleList()
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    // 分配权限
    async openpermiss(row) {
      // console.log(row.id)
      // const res = await getRoleDetailAPI(row.id)
      this.$refs.per.id = row.id
      const res = await getRoleDetailAPI(row.id)
      this.$refs.per.arr = res.permIds
      this.arr = res.permIds
      this.$refs.per.showDialog = true
      // console.log(this.$refs.per)
    }
  }
}
</script>

<style></style>
